package org.dizena.modules.task.web;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.dizena.common.pojo.PageReq;
import org.dizena.common.pojo.PageResp;
import org.dizena.modules.task.bean.JobInfo;
import org.dizena.modules.task.service.JobInfoService;
import org.dizena.modules.task.service.TaskService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 任务管理接口
 */
@RestController
@RequiresPermissions("task:job:*")
@RequestMapping("/api/v1/task/job")
public class JobInfoController
{
    @Resource
    private JobInfoService jobInfoService;
    @Resource
    private TaskService taskService;

    /**
     * 枚举
     */
    @PostMapping("list")
    public PageResp<JobInfo> list(PageReq req)
    {
        return jobInfoService.list(req);
    }

    /**
     * 增加
     */
    @PostMapping("add")
    public String add(@Validated JobInfo m)
    {
        jobInfoService.add(m);
        return "ok";

    }

    /**
     * 修改
     */
    @PostMapping("edit")
    public String edit(@Validated JobInfo m)
    {
        jobInfoService.edit(m);
        return "ok";

    }

    /**
     * 删除
     */
    @PostMapping("del")
    public String del(String id)
    {
        jobInfoService.del(id);
        return "ok";
    }

    /**
     * 启动
     */
    @PostMapping("change")
    public String change(String id)
    {
        jobInfoService.change(id);
        return "ok";
    }

    /**
     * 立即执行
     */
    @PostMapping("execute")
    public String execute(String id)
    {
        taskService.executeNow(jobInfoService.get(id));
        return "ok";
    }

}
